
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="topicName">
            <div class="item">
              <span v-html="'话题名称'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.topicName"
                placeholder="请输入话题名称"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="orderNum">
            <div class="item">
              <span v-html="'是否热门'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                v-model="searchForm.orderNum"
                placeholder="请选择是否热门"
                size="mini"
              >
                <el-option
                  v-for="item in orderNumOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item prop="showStatus">
            <div class="item">
              <span v-html="'启用状态'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                v-model="searchForm.showStatus"
                placeholder="请选择启用状态"
                size="mini"
              >
                <el-option
                  v-for="item in stautsOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="topBtn">
      <el-button
        size="mini"
        @click="addClick"
        type="success"
        plain
        icon="el-icon-plus"
        v-has="'community:topic:add'"
        >新增话题</el-button
      >
    </div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        :height="screenHeight - 400"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="topicName"
          align="center"
          label="话题名称"
        >
        <template slot-scope="scope">
          {{scope.row.topicName}}
          <span v-if='scope.row.orderNum==1' style="color:red;border:2px solid red;border-radius:4px;padding:2px 3px">精</span>
        </template>
        </el-table-column>
        <el-table-column
          prop="intro"
          align="center"
          label="话题介绍"
           width="400"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          label="创建时间"
           
        >
          <template slot-scope="scope">{{
            scope.row.createTime | formatDate
          }}</template>
        </el-table-column>
        <el-table-column
          prop="createBy"
          align="center"
          label="创建人"
           
        >
        </el-table-column>
        <el-table-column
          prop="posterNum"
          sortable
          align="center"
          label="动态数量"
            width="120"
        >
        </el-table-column>

        <el-table-column align="center" label="启用状态" width="80">
          <template slot-scope="scope">
            <el-switch
              :disabled="permissions.indexOf('community:topic:enable') == -1"
              @change="statusChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.showStatus"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'community:topic:edit'"
              @click="editClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-has="'community:topic:hot'"
              @click="hotClick(scope.row)"
            >
              <div
                v-html="scope.row.orderNum == 1 ? '取消热门' : '设为热门'"
              ></div>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      title="编辑用户"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="editAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="topicName" label="话题名称：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.topicName"
                placeholder="请输入话题名称"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="intro" label="话题介绍：">
            <div class="item">
              <el-input
                type="textarea"
                :rows="4"
                maxlength="200"
                size="mini"
                v-model="addEditForm.intro"
                placeholder="请输入话题介绍"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      hotTitle: "",
      title: "新增话题",
      orderNumOptions: [
        { label: "热门", value: 1 },
        { label: "非热门", value: 0 },
      ],
      screenHeight: document.documentElement.clientHeight,
      searchForm: {
        topicName: "",
        orderNum: "",
        showStatus: "",
      },
      orderNumFormatter: {
        1: "热门",
        0: "非热门",
      },

      stautsFormatter: {
        0: "停用",
        1: "启用",
      },
      stautsOptions: [
        { label: "启用", value: 1 },
        { label: "停用", value: 0 },
      ],
      tableLoading: false,
      tableHeight: 50,
      tableData: [],
      addEditDialogVisible: false,
      addEditForm: {
        topicName: "",
        intro: "",
      },
      addEditRules: {
        topicName: [
          { required: true, message: "话题名称不能为空", trigger: "blur" },
        ],
      },
      editId: "",
    };
  },

  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.getTopicList();
  },
  methods: {
    
    hotClick(row) {
      row.orderNum = row.orderNum === 1 ? 0 : 1;
      let data = {
        id: row.id,
        orderNum: row.orderNum,
      };
      this.$api.hotTopic(data).then((res) => {
        if (res.data.code == "00000") {
          this.$message.success("操作成功");
          this.getTopicList();
          this.addEditDialogVisible = false;
        }
      });
    },
    addClick() {
      this.addEditForm = {};
      this.title = "新增话题";
      this.addEditDialogVisible = true;
    },
    searchClick() {
      this.getTopicList();
    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.getTopicList();
    },

    /**
     * 获取话题列表
     * @param {*}
     */
    async getTopicList() {
      this.tableLoading = true;
      await this.$api
        .topicList({
          ...this.searchForm,
        })
        .then((res) => {
          this.tableData = res.data.result;
          this.tableLoading = false;
        });
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row) {
      this.title = "编辑话题";
      this.addEditDialogVisible = true;
      this.editId = row.id;
      this.addEditForm = JSON.parse(JSON.stringify(row));
    },
    /**
     * 状态更新回调
     * @param {*} row
     */
    async statusChange(row) {
      try {
        const flag = row.showStatus; // 点击之后的值
        row.showStatus = row.showStatus === 1 ? 0 : 1; // switch点击前的状态
        const confirmTips = flag === 1 ? "启用" : "停用";
        //确认框
        const confirm = await this.$confirm(
          `确定${confirmTips}话题吗？`,
          "提示",
          {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).catch(() => false);
        if (!confirm) return false;
        row.showStatus = row.showStatus === 1 ? 0 : 1;
        const res = await this.$api.statusTopic({
          id: row.id,
          showStatus: row.showStatus,
        });
        if (res.data.code == "00000") {
          this.$message.success("状态更新成功");
        }
        this.getTopicList();
      } catch (error) {
        this.$message.error(error);
      }
    },
    /**
     * 打开新增和修改的回调
     * @param {*}
     */
    addEditDialogOpen() {
      if (this.$refs.addEditFormRef) this.$refs.addEditFormRef.resetFields();
    },

    /**
     * 编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
          let data = {
            id: this.editId,
            ...this.addEditForm,
          };
          if (this.title == "新增话题") {
            data.id = "";
            await this.$api.addTopic(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("新增成功");
                this.getTopicList();
                this.addEditDialogVisible = false;
              }
            });
          } else {
            await this.$api.editTopic(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("修改成功");
                this.getTopicList();
                this.addEditDialogVisible = false;
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
   .topBtn {
    margin: 10px 0;
text-align: right;
  }
  .tableBox {
    flex: 1;
    .bottomBtn {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
  .editAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>